<?php

$m = new MongoClient('mongodb://ftp.giemiebier.nl');

$db = $m->chatdb;
$user_chats = $db->user_chats;
//echo $pagetree[2];
$cursor = $user_chats->find(array("_id" => $pagetree[2]));
//var_dump($cursor);
$count = 0;
foreach ($cursor as $document) {
    $return['ids'] = $document["chat_id"];
    foreach ($return['ids'] as $id) {
        $chats = $db->chat;
        $cursor2 = $chats->find(["_id" => new MongoId($id)]);
        foreach ($cursor2 as $chat) {
            $temp = array();
            $tmp['users'] = $chat['users'];
            
            $tmp['id'] = $chat['_id']->{'$id'};
            $return['chats'][] = $tmp;
        }
    }
    $count++;
}
if ($count == 0) {
    $return['error'] = "geen chats gevonden";
} else {
    $return['valid'] = true;
}
echo json_encode($return);